package com.example.himalayaproject.find.monthlyfocus.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.himalayaproject.R;
import com.example.himalayaproject.find.monthlyfocus.WebViewActivity;
import com.example.himalayaproject.find.monthlyfocus.entity.HeadPage;
import com.example.himalayaproject.find.monthlyfocus.entity.Month_List;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2016/12/5.
 */
public class RecycleViewAdapter extends  RecyclerView.Adapter implements View.OnClickListener {
    private Context context;
    private List<Month_List> datas;//榜单页
    private HeadPage headPage;//头条页


    private final int TYPE_HEAD=-1;
    private final int TYPE_ITEM_ONE=0;
    private final int TYPE_ITEM_TWO=1;

    public RecycleViewAdapter(Context context, List<Month_List> datas, HeadPage headPage) {
        this.context = context;
        this.datas = datas;
        this.headPage = headPage;
    }

    @Override
    public int getItemViewType(int position) {
        switch (position) {
            case 0:
                return TYPE_HEAD;
            case 1:
                return TYPE_ITEM_ONE;
            case 2:
                return TYPE_ITEM_TWO;
        }
        return -2;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=null;
        switch (viewType) {
            case TYPE_HEAD:
                view = LayoutInflater.from(context).inflate(R.layout.fragmet_head_item, parent, false);
                HeadViewHolder headViewHolder = new HeadViewHolder(view);
                return headViewHolder;
            case TYPE_ITEM_ONE:
                view = LayoutInflater.from(context).inflate(R.layout.fragmet_list_item, parent, false);
                ItemViewHolder itemViewHolderOne =  new ItemViewHolder(view);
                return itemViewHolderOne;
            case TYPE_ITEM_TWO:
                view = LayoutInflater.from(context).inflate(R.layout.fragmet_list_item, parent, false);
                ItemViewHolder itemViewHolderTwo = new ItemViewHolder(view);
                return itemViewHolderTwo;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case TYPE_HEAD:
                if(holder instanceof HeadViewHolder){
                    //在顶部位置下载图片
                    Picasso.with(context).load(headPage.getPic()).into(((HeadViewHolder) holder).iv_head);
                    ((HeadViewHolder) holder).linearLayout.setOnClickListener(this);
                }
                break;
            case TYPE_ITEM_ONE:
                if(holder instanceof ItemViewHolder) {
                    //得到榜单页的头部
                    ((ItemViewHolder) holder).tvItemTotalTitle.setText(datas.get(TYPE_ITEM_ONE).getTitle());
                    //绑定适配器
                    ((ItemViewHolder) holder).recyclerViewSecond.setAdapter(new RecycleViewSceondAdapter(context,datas.get(TYPE_ITEM_ONE)));

                }
                break;
            case TYPE_ITEM_TWO:
                if(holder instanceof ItemViewHolder) {
                    ((ItemViewHolder) holder).tvItemTotalTitle.setText(datas.get(TYPE_ITEM_TWO).getTitle());
                    ((ItemViewHolder) holder).recyclerViewSecond.setAdapter(new RecycleViewSceondAdapter(context,datas.get(TYPE_ITEM_TWO)));

                }

                break;
        }

    }

    @Override
    public int getItemCount() {
        return 3;
    }

    @Override
    public void onClick(View view) {
        int position=((RecyclerView)view.getParent()).getChildAdapterPosition(view);
        Intent intent=new Intent(context, WebViewActivity.class);
        intent.putExtra("url",headPage.getUrl());
        context.startActivity(intent);
    }

    //顶部ViewHolder
    public class HeadViewHolder extends RecyclerView.ViewHolder{

        private final ImageView iv_head;
        private LinearLayout linearLayout;

        public HeadViewHolder(View itemView) {
            super(itemView);
            iv_head = ((ImageView) itemView.findViewById(R.id.iv_find_frg_ranking_head));
            linearLayout= (LinearLayout) itemView.findViewById(R.id.layout);
        }
    }
    // 榜单项ViewHolder
    public class ItemViewHolder extends RecyclerView.ViewHolder{
        private TextView tvItemTotalTitle;
        private  RecyclerView recyclerViewSecond;

        public ItemViewHolder(View itemView) {
            super(itemView);
            recyclerViewSecond = (RecyclerView) itemView.findViewById(R.id.recycler_frg_ranking_second_id);
            tvItemTotalTitle = ((TextView) itemView.findViewById(R.id.tv_frg_ranking_item_name));


        }


    }
}
